package org.hcyspartnoc.luoxin.controller.searchtopic;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.common.CommonUtilsException;
import org.hcyspartnoc.luoxin.common.exception.searchtopic.TopicException;
import org.hcyspartnoc.luoxin.pojo.dto.searchtopic.TopicPictureDto;
import org.hcyspartnoc.luoxin.pojo.dto.searchtopic.TopicRagDto;
import org.hcyspartnoc.luoxin.pojo.dto.searchtopic.TopicTextDto;
import org.hcyspartnoc.luoxin.pojo.vo.searchtopic.QuestionVo;
import org.hcyspartnoc.luoxin.pojo.vo.searchtopic.TopicVO;
import org.hcyspartnoc.luoxin.service.searchtopic.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/search/topic")
public class ServiceSearchTopicController {

    @Autowired
    TopicService topicService;

    @Log(title = "搜题·图片·添加", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "search:topic:picture:create", descr = "添加图片转文字")
    @PostMapping("/picture/create")
    public AjaxResult createPictureTopic(@Valid @RequestParam MultipartFile image) throws TopicException, CommonUtilsException {
        TopicVO topicVO = topicService.createTopic(image);
        return AjaxResult.success(topicVO);
    }

    @Log(title = "搜题·删除·通过Id", businessType = BusinessType.DELETE,isSaveResponseData = false)
    @PreAuthorize(auth = "search:topic:picture:delete", descr = "用户删除搜题记录")
    @DeleteMapping("/picture/delete/{pictureTopicId:\\d+}")
    public AjaxResult deletePictureTopic(@NotNull @PathVariable Integer pictureTopicId) throws TopicException {
        topicService.deleteTopicByid(pictureTopicId);
        return AjaxResult.successMsg();
    }

    @Log(title = "搜题·获取·通过用户Id", businessType = BusinessType.OTHER,isSaveResponseData = false)
    @PreAuthorize(auth = "search:topic:get", descr = "用户获取搜题记录")
    @GetMapping ("/picture/get")
    public AjaxResult getPictureTopic() throws TopicException {
        List<TopicVO> pictureTopicPoList = topicService.getPictureTopic();
        return AjaxResult.success(pictureTopicPoList);
    }
    @Log(title = "搜题·文字·添加", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "search:topic:text:create", descr = "用户添加文字搜题")
    @PostMapping ("/text/create")
    public AjaxResult createTextTopic(@Valid @RequestBody TopicTextDto topicTextDto) throws TopicException {
        TopicVO topicVO = topicService.createTextTopic(topicTextDto);
        return AjaxResult.success(topicVO);
    }

    @Log(title = "搜题·文字·解析·Rag", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "search:topic:analysis:rag", descr = "通过Rag获取解析")
    @PostMapping ("/analysis/rag")
    public AjaxResult searchTopicByRag(@Valid @RequestBody TopicRagDto topicRagDto) throws TopicException {
        QuestionVo questionVo = topicService.searchTopicByRag(topicRagDto);
        return AjaxResult.success(questionVo);
    }
}
